package com.cindata

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

import com.cindata.dictionary.*

@Secured(['isAuthenticated()'])
@Transactional
class RiskWarningFlowStageController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def springSecurityService

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond RiskWarningFlowStage.list(params), model:[riskWarningFlowStageCount: RiskWarningFlowStage.count()]
    }

    def show(RiskWarningFlowStage riskWarningFlowStage) 
    {
        def user = User.findById(springSecurityService.principal.id)
        def roleSql = ""
        def roleList = UserRole.findAllByUser(user)*.role
        if (roleList) 
        {
            String roleIdList = roleList*.id?.toString()
            roleSql += " and role.id in (" + roleIdList?.substring(1, roleIdList.lastIndexOf("]")) + ")"
        }

        def editRoleFlag = RiskWarningFlowStageRole.find("from RiskWarningFlowStageRole where flowStage.id = ? and teamRole.id = ? " + roleSql, [riskWarningFlowStage?.id, TeamRole.EDIT()?.id]) && true
        def approveRoleFlag = RiskWarningFlowStageRole.find("from RiskWarningFlowStageRole where flowStage.id = ? and teamRole.id = ? " + roleSql, [riskWarningFlowStage?.id, TeamRole.APPROVAL()?.id]) && true

        respond riskWarningFlowStage, model: [editRoleFlag: editRoleFlag, approveRoleFlag: approveRoleFlag]
    }

    def create() 
    {
        params['targetUri'] = request.getHeader("referer")
        respond new RiskWarningFlowStage(params)
    }

    @Transactional
    def save(RiskWarningFlowStage riskWarningFlowStage) 
    {
        if (riskWarningFlowStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningFlowStage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningFlowStage.errors, view:'create'
            return
        }

        riskWarningFlowStage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'riskWarningFlowStage.label', default: 'RiskWarningFlowStage'), riskWarningFlowStage.id])
                redirect url: params['targetUri']
            }
            '*' { respond riskWarningFlowStage, [status: CREATED] }
        }
    }

    def edit(RiskWarningFlowStage riskWarningFlowStage) 
    {
        params['targetUri'] = request.getHeader("referer")
        respond riskWarningFlowStage
    }

    @Transactional
    def update(RiskWarningFlowStage riskWarningFlowStage) {
        if (riskWarningFlowStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningFlowStage.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningFlowStage.errors, view:'edit'
            return
        }

        riskWarningFlowStage.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'riskWarningFlowStage.label', default: 'RiskWarningFlowStage'), riskWarningFlowStage.id])
                redirect url: params['targetUri']
            }
            '*'{ respond riskWarningFlowStage, [status: OK] }
        }
    }

    @Transactional
    def delete(RiskWarningFlowStage riskWarningFlowStage) {

        if (riskWarningFlowStage == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        riskWarningFlowStage.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'riskWarningFlowStage.label', default: 'RiskWarningFlowStage'), riskWarningFlowStage.id])
                redirect url: request.getHeader("referer")
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'riskWarningFlowStage.label', default: 'RiskWarningFlowStage'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
